﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SC2BuildOrderQuickie
{
    public partial class Form1 : Form
    {
        App App = new App();
        QuickChart[] charts = new QuickChart[4];

        public Form1()
        {
            InitializeComponent();
            charts[0] = new QuickChart( 1, panel1 );
            charts[1] = new QuickChart( 2, panel2);
            charts[2] = new QuickChart( 3, panel3);
            charts[3] = new QuickChart( 4, panel4);
            charts[0].RolloverBucket += HighlightBuild;
            charts[1].RolloverBucket += HighlightBuild;
            charts[2].RolloverBucket += HighlightBuild;
            charts[3].RolloverBucket += HighlightBuild;
        }

        private void HighlightBuild( BOCode bo_code ) {
            String best_filename = bo_code.GetFileName();
            String[] best = File.ReadAllLines(UI_Txt_BODir.Text + "\\" + best_filename);

            richTextBox1.Text = ReformatLog( best );
        }

        private void button1_Click(object sender, EventArgs e) {
            button1.Enabled = false;

            List<String> lines = new List< string >( File.ReadLines( UI_Txt_DB.Text ) );
            App.AllBuilds = new List< BOCode >();
            foreach( String line in lines ) {
                BOCode build = new BOCode( line );
                App.AllBuilds.Add( build );
                App.PartitionList.Include( build );
            }
            App.AllBuilds.Sort(delegate( BOCode dna, BOCode order_dna ) {
                                   return dna.Player.CompareTo( order_dna.Player );
                               });
            UI_Txt_DbSize.Text = App.AllBuilds.Count.ToString();
            listBox1.DataSource = App.AllBuilds;
        }

        
        private void button2_Click(object sender, EventArgs e) {
            BOCode build_order;
            build_order = (BOCode)listBox1.SelectedItem;
            int SECOND_CHUNKS = 5;
            if (build_order == null) return;

            for( int i=0 ; i+1 < build_order.Length && i < charts.Length ; i++ ) {
                int offset = i + 1;
                var partition = App.PartitionList.Get(offset, build_order.GetSubset(offset + 1));
                if( partition == null ) {
                    break;
                }
                charts[i].Counter = new FrequencyCounter(offset, partition, SECOND_CHUNKS);
                charts[i].SelectedSecondBucket = build_order.SecondBuckets[offset] / SECOND_CHUNKS;
            }
            int endpoint = Math.Min( build_order.Length - 1, charts.Length );
            var end_partition = App.PartitionList.Get( endpoint, build_order.GetSubset(endpoint+1));

            build_code.Text = (build_order.GetSubset(endpoint+1));
            String best_filename = end_partition.Builds[ 0 ].GetFileName();
            String my_filename = build_order.GetFileName();
            String[] best = File.ReadAllLines(UI_Txt_BODir.Text + "\\" + best_filename);
            String[] my = File.ReadAllLines(UI_Txt_BODir.Text + "\\" + my_filename);
            richTextBox1.Text = ReformatLog( best );
            richTextBox2.Text = ReformatLog( my );
        }

        private static String ReformatLog( string[] lines ) {
            StringBuilder sb = new StringBuilder();
            bool is_dump = false;
            for( int i=0 ;i < lines.Length ; i++ ) {
                if (String.IsNullOrWhiteSpace(lines[i])) continue;

                if (is_dump == false) {
                    sb.Append( lines[ i ] ).AppendLine();
                    if (lines[i].StartsWith("BEGIN DUMP")) is_dump = true;
                    continue;
                }
                

                String[] tokens = lines[ i ].Split( '|' );
                TimeSpan time = new TimeSpan( 0,0,0,int.Parse( tokens[ 0 ] ));
                String command = tokens[ 3 ];
                string hex = tokens[ 2 ];
                string target = tokens[ 5 ];
                string loc = tokens[ 6 ];
                bool is_suspected_dupe = bool.Parse(tokens[tokens.Length-1]);
                if (is_suspected_dupe) continue;
                sb.Append( time.ToString() ).Append( ':' )
                    .Append( command ).Append( '{' ).Append( target ).Append( loc ).Append( '}' ).AppendLine();
            }
            return sb.ToString();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button2_Click( null, null );

        }
    }
}
